<!DOCTYPE html>
<html lang="zh-cmn-Hans" prefix="og: http://ogp.me/ns#" class="han-init">
<head>
  <meta charset="utf-8">
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
  <link rel="icon" href="http://upeng.github.io/favicon.ico">
  <title>upeng</title>
  <link rel="stylesheet" href="/css/style.css">
  <link rel="stylesheet" href="/lib/fancybox/jquery.fancybox-1.3.4.css">
  <!--在这里倒入jquery 方便处理部分页面的jquery-->
  <script src="https://cdn.staticfile.org/jquery/1.7/jquery.min.js" type="text/javascript" ></script>
</head>

<body>
	<header class="site-header navfixed-false">
  <div class="container">
      <h1><a href="/" title="upeng"><span class="octicon octicon-mark-github"></span> upeng</a></h1>
      <nav class="site-header-nav" role="navigation">
        
              
              <a href="/"  class=" site-header-nav-item hvr-underline-from-center" title="Home">Home</a>
        
              
              <a href="/categories/"  class=" site-header-nav-item hvr-underline-from-center" title="Categories">Categories</a>
        
              
              <a href="/bookmark/"  class=" site-header-nav-item hvr-underline-from-center" title="Bookmark">Bookmark</a>
        
              
              <a href="http://shareup.sinaapp.com"  class=" site-header-nav-item hvr-underline-from-center" title="Share">Share</a>
        
              
              <a href="/about/"  class=" site-header-nav-item hvr-underline-from-center" title="About">About</a>
        
      </nav>
  </div>
</header>

	
<section class="collection-head geopattern" data-pattern-id="mysql数据库之索引优化" >
    <div class="container">
        <div class="collection-title">
            <h1 class="collection-header">
                mysql数据库之索引优化
            </h1>
            
                <div class="collection-info">
                    <span class="meta-info">
                        <span class="octicon octicon-calendar"></span>
                        <time datetime="2016-04-26T16:44:33.000Z" itemprop="datePublished">2016-04-27</time>
                    </span>
                    
                        <span class="meta-info">
                            <span class="octicon octicon-file-directory"></span>
                            <a href='/categories/MySQL/' title=''>MySQL</a>
                        </span>
                    
                </div>
            
        </div>
    </div>
</section>
	<section class="container">
    <div class="columns">
        <!-- -->
        <div class="column three-fourths">
            <article class="article-content markdown-body">
                <h2 id="如何选择合适的列建立索引"><a href="#如何选择合适的列建立索引" class="headerlink" title="如何选择合适的列建立索引?"></a>如何选择合适的列建立索引?</h2><ol>
<li>在where从句、group by从句、order by从句、on从句中出现的列</li>
<li>索引字段越小越好</li>
<li>离散度大的列放到联合索引的前面</li>
</ol>
<blockquote>
<p>理解离散度</p>
<pre><code>SELECT * FROM payment where staff_id = 2 AND customer_id = 584;
</code></pre><p>是index(staff_id, customer_id)好还是index(customer_id, staff_id)好？</p>
</blockquote>
<p><strong>备注</strong>：比较staff_id和customer_id两者的离散度，那个离散度高，那个列就放置到联合索引的前面<br><a id="more"></a></p>
<blockquote>
<p>计算离散度：</p>
</blockquote>
<pre><code>SELECT count(distinct staff_id), count(distinct customer_id) FROM payment;
</code></pre><h2 id="索引优化SQL的方法"><a href="#索引优化SQL的方法" class="headerlink" title="索引优化SQL的方法"></a>索引优化SQL的方法</h2><h3 id="索引的维护及优化—重复及冗余索引"><a href="#索引的维护及优化—重复及冗余索引" class="headerlink" title="索引的维护及优化—重复及冗余索引"></a>索引的维护及优化—重复及冗余索引</h3><blockquote>
<p>重复索引</p>
</blockquote>
<p>重复索引是指相同的列以相同的顺序建立同类型的索引，如下表中primary key 和ID列上的索引就是重复索引</p>
<pre><code>create table test(
    id int not null primary key,
    name varchar(10) not null,
    titile varchar(50) not nullm,
    unique(id)
)engine=innodb;
</code></pre><blockquote>
<p>冗余索引</p>
</blockquote>
<p>冗余索引是指多个索引的前缀列相同，或是在联合索引中包含了主键的索引，下面这个例子中key(name, id)就是一个冗余索引</p>
<pre><code>create table test(
    id int not null primary key,
    name varchar(10) not null,
    titile varchar(50) not nullm,
    key(name, id)
)engine=innodb;
</code></pre><blockquote>
<p>工具检查重复及冗余索引</p>
</blockquote>
<pre><code>pt-duplicate-key-checker -uroot  -p 123 -h 127.0.0.1
</code></pre><h3 id="索引的维护及优化—删除不用索引"><a href="#索引的维护及优化—删除不用索引" class="headerlink" title="索引的维护及优化—删除不用索引"></a>索引的维护及优化—删除不用索引</h3><p> 目前mysql中还没有记录索引的使用情况，但是在perconMySQL和MariaDB中可以通过INDEX_STATISTICS表来查看哪些索引未使用，但是在MySQL中目前只能通过慢查询日志配合pt-index-usage工具来进行索引使用情况的分析</p>
<pre><code>pt-index-usage -uroot -p123 mysql-slow.log
</code></pre>
            </article>
            
                <div class="share">
                    <!--开启分享-->
<div class="share-component" data-disabled="google,twitter,facebook" data-description="如何选择合适的列建立索引?
在where从句、grou..."></div>

<script src="/js/share.min.js"></script>

                </div>    
            
            
                
<div class="comments">
	<div class="ds-thread" data-thread-key="mysqlperformance03" data-title="mysql数据库之索引优化" data-url="http://upeng.github.io/2016/04/27/mysqlperformance03/"></div>
	<script type="text/javascript">
	var duoshuoQuery = {short_name:"upeng"};
		(function() {
			var ds = document.createElement('script');
			ds.type = 'text/javascript';ds.async = true;
			ds.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') + '//static.duoshuo.com/embed.js';
			ds.charset = 'UTF-8';
			(document.getElementsByTagName('head')[0]
			 || document.getElementsByTagName('body')[0]).appendChild(ds);
		})();
	</script>
</div>

            
        </div>
        <div class="column one-fourth">
            
                
                


<h3>Post Directory</h3>

<div id="post-directory-module">
	<section class="post-directory">
		<p><strong class="toc-title">文章目录</strong></p>
		<ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#如何选择合适的列建立索引"><span class="toc-text">如何选择合适的列建立索引?</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#索引优化SQL的方法"><span class="toc-text">索引优化SQL的方法</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#索引的维护及优化—重复及冗余索引"><span class="toc-text">索引的维护及优化—重复及冗余索引</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#索引的维护及优化—删除不用索引"><span class="toc-text">索引的维护及优化—删除不用索引</span></a></li></ol></li></ol>
	</section>
</div>
            
        </div>
    </div>
</section>

<footer class="container">
    <div class="site-footer" role="contentinfo">
        <div class="copyright left mobile-block">
                © 2016
                <span title="yupeng">yupeng</span>
                <a href="javascript:window.scrollTo(0,0)" class="right mobile-visible">TOP</a>
        </div>

        <ul class="site-footer-links right mobile-hidden">
            <li>
                <a href="javascript:window.scrollTo(0,0)" >TOP</a>
            </li>
        </ul>

        <a href="https://github.com/upeng" target="_blank" aria-label="view source code">
            <span class="mega-octicon octicon-mark-github" title="GitHub"></span>
        </a>

        <ul class="site-footer-links mobile-hidden">
            
                  
                  <li>
                    <a href="/"  title="Home">Home</a>
                  </li>
            
                  
                  <li>
                    <a href="/categories/"  title="Categories">Categories</a>
                  </li>
            
                  
                  <li>
                    <a href="/bookmark/"  title="Bookmark">Bookmark</a>
                  </li>
            
                  
                  <li>
                    <a href="http://shareup.sinaapp.com"  title="Share">Share</a>
                  </li>
            
                  
                  <li>
                    <a href="/about/"  title="About">About</a>
                  </li>
            
            <li>
                <a href="/atom.xml">
                    <span class="octicon octicon-rss" style="color:orange;"></span>
                </a>
            </li>
        </ul>
    </div>
</footer>


		<script src="/js/geopattern.js"></script>

		
			<script src="/js/toc.js"></script>
		

		<script src="/js/highlight.pack.js"></script>
		<script src="/lib/fancybox/jquery.fancybox-1.3.4.pack.js"></script>

		<script src="/js/index.js"></script>

		 <script src="/js/popular_repo.js"></script> 

	</body>
</html>